// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/common/selective_column_reader_internal.h>
#include <pollux/dwio/dwrf/common/decoder_util.h>
#include <pollux/dwio/dwrf/reader/dwrf_data.h>

namespace kumo::pollux::dwrf {
class SelectiveTimestampColumnReader
    : public dwio::common::SelectiveColumnReader {
 public:
  // The readers produce int64_t, the vector is Timestamps.
  using ValueType = int64_t;

  SelectiveTimestampColumnReader(
      const std::shared_ptr<const dwio::common::TypeWithId>& fileType,
      DwrfParams& params,
      common::ScanSpec& scanSpec);

  void seekToRowGroup(int64_t index) override;
  uint64_t skip(uint64_t numValues) override;

  void read(int64_t offset, const RowSet& rows, const uint64_t* incomingNulls)
      override;

  void getValues(const RowSet& rows, VectorPtr* result) override;

 private:
  template <bool isDense>
  void readHelper(common::Filter* filter, const RowSet& rows);

  void
  processNulls(const bool isNull, const RowSet& rows, const uint64_t* rawNulls);
  void processFilter(
      const common::Filter* filter,
      const RowSet& rows,
      const uint64_t* rawNulls);

  const TimestampPrecision precision_;

  std::unique_ptr<dwio::common::IntDecoder</*isSigned*/ true>> seconds_;
  std::unique_ptr<dwio::common::IntDecoder</*isSigned*/ false>> nano_;

  // Values from copied from 'seconds_'. Nanos are in 'values_'.
  BufferPtr secondsValues_;
  RleVersion version_;
};

} // namespace kumo::pollux::dwrf
